//
//  ClubRealVerfityViewController.swift
//  77Club
//
//  Created by Apple on 2024/9/20.
//

import UIKit

class ClubRealVerfityViewController: ClubBaseViewController {

    private let bgImageView = c_make_ex(UIImageView.self)
        .image("Mine_verfity_bg").end()
    private let backButton = c_make_ex(UIButton.self)
        .image("Common_blackBack").end()
    private let titleLabel = c_make_ex(UILabel.self)
        .text("实名认证").color(.black).font(16.font_semiBold).end()
    private lazy var exploreLabel    = ActiveLabel()
    private let contentImageView = c_make_ex(UIImageView.self)
        .image("Mine_verfity_content").end()
    private let nameTextField = c_make_ex(UITextField.self)
        .placeholder("请输入真实姓名").corner(25).bgColor(.white)
        .color(.black).font(14.font_medium).end()
    private let idCardField = c_make_ex(UITextField.self)
        .placeholder("请输入本人真实身份证号码").corner(25).bgColor(.white)
        .color(.black).font(14.font_medium).end()
    private let verfityButton = c_make_ex(UIButton.self)
        .bgImage(UIImage.colorToImage(color: "#FFC99A".toColor), state: .disabled)
        .bgImage(UIImage.colorToImage(color: "#FF831A".toColor))
        .color(.white).text("立即认证").font(16.font_semiBold)
        .corner(22).end()
    private let bottomView = c_make_ex(UIView.self)
        .bgColor("#F2F3F5").corner(8).end()
    private let alertImageView = c_make_ex(UIImageView.self)
        .image("Mine_verfity_alert").end()
    private let alertLabel = c_make_ex(UILabel.self)
        .color("#7B7D80").font(12.font_medium)
        .text("实名认证仅做防沉迷功能，对账号安全没有任何影响").end()
    
    var updateUserInfoData: (() -> Void)?
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.subviews{
            bgImageView
            backButton
            titleLabel
            exploreLabel
            contentImageView.subviews{
                nameTextField
                idCardField
                verfityButton
            }
            bottomView.subviews{
                alertImageView
                alertLabel
            }
        }
        
        bgImageView.fillContainer()
        backButton.leading(0).top(.statusHeight).width(56).height(44)
        titleLabel.Leading == backButton.Trailing
        titleLabel.CenterY == backButton.CenterY
        exploreLabel.leading(12.0.Scale).trailing(12.0.Scale).top(.navi_height + 16.0.Scale)
        contentImageView.leading(12.0.Scale).trailing(12.0.Scale).height(265.0.Scale)
        contentImageView.Top == exploreLabel.Bottom + 24.0.Scale
        nameTextField.leading(12.0.Scale).trailing(12.0.Scale).top(30.0.Scale).height(50)
        idCardField.leading(12.0.Scale).trailing(12.0.Scale).height(50)
        idCardField.Top == nameTextField.Bottom + 24.0.Scale
        verfityButton.leading(12.0.Scale).trailing(12.0.Scale).height(44)
        verfityButton.Top == idCardField.Bottom + 48.0.Scale
        bottomView.leading(12.0.Scale).trailing(12.0.Scale).height(44)
        bottomView.Top == contentImageView.Bottom + 16.0.Scale
        alertImageView.leading(12.0.Scale).size(20.0.Scale).centerVertically()
        alertLabel.leading(36.0.Scale).centerVertically()
        
        configActiveStyle()
        contentImageView.isUserInteractionEnabled = true
        verfityButton.isEnabled = false
        
        nameTextField.style { v in
            v.placehold("#7B7D80".toColor, font: 14.font_medium)
            let leftV = UIView(frame: CGRect(x: 0, y: 0, width: 16, height: 16))
            v.leftView = leftV
            v.leftViewMode = .always
            v.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
        }
        idCardField.style { v in
            v.placehold("#7B7D80".toColor, font: 14.font_medium)
            let leftV = UIView(frame: CGRect(x: 0, y: 0, width: 16, height: 16))
            v.leftView = leftV
            v.leftViewMode = .always
            v.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
        }
        
        backButton.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
        verfityButton.addTarget(self, action: #selector(clickVerfityButton), for: .touchUpInside)
    }
    
    private func configActiveStyle() {
        let customType = ActiveType.custom(pattern: "乐玩")
        let customType1 = ActiveType.custom(pattern: "谨慎填写")
        exploreLabel.isShowUnderLine = false
        exploreLabel.font = 14.font_medium
        exploreLabel.numberOfLines = 0
        exploreLabel.textAlignment = .left
        exploreLabel.enabledTypes = [customType, customType1]
        exploreLabel.text = "根据国家相关法律法规，您的所填写的信息乐玩将用于为您提供网络游戏防沉迷认证、保护未成年人权益、信息发布等服务\n该信息将作为您的账号认证信息，请务必谨慎填写"
        exploreLabel.textColor = "#7B7D80".toColor
        exploreLabel.customColor[customType] = "#FF7700".toColor
        exploreLabel.customColor[customType1] = "#FF7700".toColor
    }
}


extension ClubRealVerfityViewController: UITextFieldDelegate {
    
    @objc private func clickVerfityButton(){
        
        guard let name = nameTextField.text else {
            "真实姓名不能为空".toast_error
            return
        }
        guard let icCard = idCardField.text else {
            "身份证不能为空".toast_error
            return
        }
        
        let dict = ["name": name, "idcard": icCard]
        guard let jsonStr = ClubGlobalTool.shared.convertToJsonData(dict) else { return }
        guard let encryStr = ClubGlobalTool.shared.AES128Encrypt(plainText: jsonStr, key: "mtetbpryjhdxmnhx") else { return }
        Task {
            let res = await network.requestSpecial(.realVerfity(value: encryStr))
            if res.isSuccess {
                updateUserInfoData?()
                self.navigationController?.popViewController(animated: true)
            }
        }
    }
    
    // 当textField内容变化时调用的方法
    @objc private func textFieldDidChange(_ textField: UITextField) {
        updateVerfityButtonState()
    }
    
    private func updateVerfityButtonState(){
        
        if nameTextField.text?.count ?? 0 > 0 && idCardField.text?.count ?? 0 > 0 {
            verfityButton.isEnabled = true
        }else{
            verfityButton.isEnabled = false
        }
    }
}
